using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Reflection;

using SBPweb.Data;

namespace SBPweb.Controls.Web
{

	/// <summary>
	/// This class implements a generic list that can contain entities
	/// </summary>
	/// <typeparam name="T">Entity class</typeparam>
	public class EntityListSource<T> : List<T>, IList, IEntityListSource where T : Entity
	{

		#region Constructor logic

		/// <summary>
		/// Initializes a new EntityListSource instance
		/// </summary>
		/// <param name="list">List of entities</param>
		public EntityListSource(List<T> list)
		{
			base.AddRange(list);
		}

		#endregion

		#region IEntityListSource Members

		/// <summary>
		/// Returns the elements of the list as an array
		/// </summary>
		public Array AsArray
		{
			get
			{
				return base.ToArray();
			}
		}

		/// <summary>
		/// Returns the elements of the list as an IList
		/// </summary>
		public IList AsList
		{
			get
			{
				return this;
			}
		}

		/// <summary>
		/// Returns the type of the elements
		/// </summary>
		public Type ItemType
		{
			get
			{
				return typeof(T);
			}
		}

		/// <summary>
		/// Returns true
		/// </summary>
		public bool ContainsListCollection
		{
			get
			{
				return true;
			}
		}

		/// <summary>
		/// Returns this as IList
		/// </summary>
		/// <returns></returns>
		public IList GetList()
		{
			return this;
		}

		/// <summary>
		/// This method sorts the list by the given expression
		/// </summary>
		/// <param name="expression">Comparison expression</param>
		/// <param name="direction">Directon of sorting</param>
		public void Sort(string expression, SortDirection direction)
		{
			base.Sort(new EntityGenericComparer<T>(expression, direction));
		}

		#endregion
	}
}